import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class Leetcode1592 {
    public static void main(String[] args) {

    }

    private static final String BLANk = " ";

    public static String reorderSpaces(String text) {
        int blankNumber = text.length() - text.trim().replaceAll("\\s+", "").length();

        String[] s1 = text.trim().split("\\s+");

        return s1.length == 1 ? s1[0] + BLANk.repeat(blankNumber) : String.join(BLANk.repeat(blankNumber / (s1.length - 1)), s1) + BLANk.repeat(blankNumber % (s1.length - 1));
    }
}

